<?php
/**
 * lzp300 2015-01-27
 */
class cms extends actionAbstract {
	
    /**
     * 构造
     */
    function __construct() {
        parent::__construct();
        if(!isset($_COOKIE['qingMg_admininfo'])){ header('location:'.$this->url('common/login'));}
    }
    
    //模型管理
    public function model(){
		$page=isset($_GET['page'])?((int)($_GET['page'])?(int)($_GET['page']):1):1;
        $keywords=isset($_GET['keywords'])?trim($_GET['keywords']):'';
		
		$sql_from=" from cms_model ";
		$sql_where="";
		$sql_order_by=" order by created desc";
		$field_reto=" count(*)";
		$field_re=" * ";
		
		if($keywords){
            $keywordsArr=explode(',',$keywords);
			$sql_where.=" where (";
            foreach($keywordsArr as $k=>$v){
				if($k==0){$sql_where.="title like '%".trim($v)."%'";}
				else{$sql_where.=" or title like '%".trim($v)."%'";}
            }	
			$sql_where.=")";
        }
		
		$return=$this->getPage($sql_from,$sql_where,$sql_order_by,$field_reto,$field_re,"cms","model",$page,10);
		$re=$return['re'];$number=$return['number'];$pageHtml=$return['pageHtml'];
		
		include $this->loadWidget('masterTheme');
    }
    //添加、修改模型
    public function upModel(){
        $this->loadModel('cms','model');
		$kid=isset($_POST['kid'])?(int)($_POST['kid']):0;
		$data['title']=isset($_POST['title'])?trim($_POST['title']):'';
		$data['describe']=isset($_POST['describe'])?trim($_POST['describe']):'';
		$data['status']=isset($_POST['status'])?(int)($_POST['status']):2;
		$data['created']=date('Y-m-d H:i:s',time());
		if(!$data['title']){exit(json_encode(array('state' => 0, 'html' => '请填写标题')));}
		if($kid){$re=$this->cms->modelModel->update($data,"id=$kid");}
		else{$re=$this->cms->modelModel->insert($data);}
        if($re){exit(json_encode(array('state' => 1, 'html' => '保存成功，2秒后自动刷新')));}
		exit(json_encode(array('state' => 0, 'html' => '保存失败')));
		
        include $this->loadWidget('masterTheme');
    }
	//删除模型
	function delModel(){
        $this->loadModel('cms','model');
		$kid=isset($_POST['kid'])?(int)($_POST['kid']):0;
		$re=$this->cms->modelModel->delete("id=$kid");
		if($re){exit(json_encode(array('state' => 1, 'html' => '删除成功，2秒后自动刷新')));}
		else{exit(json_encode(array('state' => 0, 'html' => '删除失败')));}
	}
    
    //cms文章管理
    public function info(){
		$page=isset($_GET['page'])?((int)($_GET['page'])?(int)($_GET['page']):1):1;
        $keywords=isset($_GET['keywords'])?trim($_GET['keywords']):'';
		
		$sql_from=" from cms_info as a left join cms_model as b on b.id=a.modelid ";
		$sql_where="";
		$sql_order_by=" order by a.modelid,a.created desc";
		$field_reto=" count(*)";
		$field_re=" a.*,b.title as motle ";
		
		if($keywords){
            $keywordsArr=explode(',',$keywords);
			$sql_where.=" where (";
            foreach($keywordsArr as $k=>$v){
				if($k==0){$sql_where.="a.title like '%".trim($v)."%' or b.title like '%".trim($v)."%'";}
				else{$sql_where.=" or a.title like '%".trim($v)."%' or b.title like '%".trim($v)."%'";}
            }	
			$sql_where.=")";
        }
		
		$return=$this->getPage($sql_from,$sql_where,$sql_order_by,$field_reto,$field_re,"cms","info",$page,10);
		$re=$return['re'];$number=$return['number'];$pageHtml=$return['pageHtml'];
		
		include $this->loadWidget('masterTheme');
    }
    //修改、添加文章
    public function infoEdit(){
        $this->loadModel('cms','model');
        $this->loadModel('cms','info');
		$model=$this->cms->modelModel->fetchAll("select id,title from cms_model where status=1");
        if($_GET){
            $kid=isset($_GET['kid'])?$_GET['kid']:0;
            $result=$this->cms->infoModel->fetchRow("select * from cms_info where id=$kid");
        }
        if($_POST){
			$kid=isset($_POST['kid'])?(int)($_POST['kid']):0;
			$data['title']=isset($_POST['title'])?trim($_POST['title']):'';
			$data['modelid']=isset($_POST['modelid'])?(int)($_POST['modelid']):0;
			$data['imgurl']=isset($_POST['imgurl'])?trim($_POST['imgurl']):'';
			$data['texturl']=isset($_POST['texturl'])?trim($_POST['texturl']):'';
			$data['briefing']=isset($_POST['briefing'])?trim($_POST['briefing']):'';
			$data['contentTxet']=isset($_POST['contentTxet'])?htmlspecialchars($_POST['contentTxet']):'';
			$data['status']=isset($_POST['status'])?(int)($_POST['status']):2;
			$data['ktime']=isset($_POST['ktime'])?trim($_POST['ktime']):'';
			$data['created']=date('Y-m-d H:i:s',time());
			if(!$data['title']){exit(json_encode(array('state' => 0, 'html' => '请填写标题')));}
			if(!$data['modelid']){exit(json_encode(array('state' => 0, 'html' => '请选择类别')));}
			if(!$data['briefing']){exit(json_encode(array('state' => 0, 'html' => '请填写简介')));}
			if(!$data['contentTxet']){exit(json_encode(array('state' => 0, 'html' => '请填写内容')));}
			if(!$data['ktime']){exit(json_encode(array('state' => 0, 'html' => '请填选择时间')));}
			if($kid){$re=$this->cms->infoModel->update($data,"id=$kid");}
			else{$re=$this->cms->infoModel->insert($data);}
			if($re){exit(json_encode(array('state' => 1, 'html' => '保存成功，2秒后自动返回列表页')));}
			exit(json_encode(array('state' => 0, 'html' => '保存失败')));
        }
        
        include $this->loadWidget('masterTheme');
    }
    //删除文章
    public function delInfo(){
        $this->loadModel('cms','info');
		$kid=isset($_POST['kid'])?(int)($_POST['kid']):0;
		$re=$this->cms->infoModel->delete("id=$kid");
		if($re){exit(json_encode(array('state' => 1, 'html' => '删除成功，2秒后自动刷新')));}
		else{exit(json_encode(array('state' => 0, 'html' => '删除失败')));}
    }
	
	//删除图片
	function urlSet(){
		$url=isset($_POST['url'])?trim($_POST['url']):'';
		$arr=explode('@',$url);
		foreach($arr as $k=>$v){
			if(trim($v)){@unlink($_SERVER['DOCUMENT_ROOT'].$v);}
		}
	}
	//焦点图管理
	function picture(){
		$page=isset($_GET['page'])?((int)($_GET['page'])?(int)($_GET['page']):1):1;
        $keywords=isset($_GET['keywords'])?trim($_GET['keywords']):'';
		
		$sql_from=" from cms_picture ";
		$sql_where="";
		$sql_order_by=" order by created desc";
		$field_reto=" count(*)";
		$field_re=" * ";
		
		if($keywords){
            $keywordsArr=explode(',',$keywords);
			$sql_where.=" where (";
            foreach($keywordsArr as $k=>$v){
				if($k==0){$sql_where.="title like '%".trim($v)."%'";}
				else{$sql_where.=" or title like '%".trim($v)."%'";}
            }	
			$sql_where.=")";
        }
		
		$return=$this->getPage($sql_from,$sql_where,$sql_order_by,$field_reto,$field_re,"cms","picture",$page,10);
		$re=$return['re'];$number=$return['number'];$pageHtml=$return['pageHtml'];
		
		include $this->loadWidget('masterTheme');
	}
	//添加修改焦点图
	function upPicture(){
		$this->loadModel('cms','picture');
		$kid=isset($_POST['kid'])?(int)($_POST['kid']):0;
		$src=isset($_POST['src'])?trim($_POST['src']):'';
		$data['title']=isset($_POST['title'])?trim($_POST['title']):'';
		$data['url']=isset($_POST['url'])?trim($_POST['url']):'';
		$data['status']=isset($_POST['status'])?(int)($_POST['status']):2;
		if(!$data['title']){exit(json_encode(array('state' => 0, 'html' => '请填写标题')));}
		if(!$kid&&!$src){exit(json_encode(array('state' => 0, 'html' => '请上传图片')));}
		$data['created']=date('Y-m-d H:i:s',time());
		if($kid){
			$old_src=$this->cms->pictureModel->fetchRow("select src from cms_picture where id=$kid");
			if(!$old_src){exit(json_encode(array('state' => 0, 'html' => '请上传图片')));}
			if($src){$data['src']=$src;@unlink($_SERVER['DOCUMENT_ROOT'].$old_src['src']);}
			$re=$this->cms->pictureModel->update($data,"id=$kid");
		}
		else{
			$data['src']=$src;
			$re=$this->cms->pictureModel->insert($data);
		}
		if($re){exit(json_encode(array('state' => 1, 'html' => '保存成功，2秒后自动刷新')));}
		exit(json_encode(array('state' => 0, 'html' => '保存失败')));
	}
	//删除焦点图
	function delPicture(){
		$this->loadModel('cms','picture');
		$kid=isset($_POST['kid'])?(int)($_POST['kid']):0;
		$re=$this->cms->pictureModel->delete("id=$kid");
		if($re){exit(json_encode(array('state' => 1, 'html' => '删除成功，2秒后自动刷新')));}
		else{exit(json_encode(array('state' => 0, 'html' => '删除失败')));}
	}
}